<?php


	class WS_Photo_Upload {
		
		public function getExtension($strImage) {
			return strtolower ( end ( explode ('.', $strImage) ) );
		}
		
		public function resizeUpload($arrProperties, $dirThumbnail) {
			$max_upload_width 	= $arrProperties['width'];
			$max_upload_height 	= $arrProperties['height'];
			$imageType 			= $arrProperties['type'];
			$tempName 			= $arrProperties['temp_name'];
			$excryptImage 		= $arrProperties['file_name'];
			
			switch($imageType) {
				case 'image/jpeg'	:
				case 'image/pjpeg'	:	$image_source = imagecreatefromjpeg($tempName);		break;
				case 'image/gif'	:	$image_source = imagecreatefromgif($tempName);		break;
				case 'image/bmp'	:	$image_source = imagecreatefromwbmp($tempName);		break;
				case 'image/png'	:	
				case 'image/x-png'	:	$image_source = imagecreatefrompng($tempName);		break;
			}
			
			#Upload Image Directory
			
			$remote_file = $dirThumbnail.$excryptImage;
			imagejpeg($image_source,$remote_file,100);
			chmod($remote_file,0644);
			list($image_width, $image_height) = getimagesize($remote_file);
			if ( $image_width > $max_upload_width || $image_height > $max_upload_height ) {
				$proportions 	= $image_width/$image_height;
				if($image_width>$image_height){
					$new_width 	= $max_upload_width;
					$new_height = round($max_upload_width/$proportions);
				}
				else {
					$new_height = $max_upload_height;
					$new_width 	= round($max_upload_height*$proportions);
				}
				
				$new_image 		= imagecreatetruecolor($new_width , $new_height);
				$image_source 	= imagecreatefromjpeg($remote_file);
				imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
				imagejpeg($new_image,$remote_file,100);
				imagedestroy($new_image);
			}
			imagedestroy($image_source);
		}
		
		public function moveToFile($imageProperties) {
			
				$tempName 		= $imageProperties['temp_name'];
				$excryptImage 	= $imageProperties['name'];
				$directory		= $imageProperties['directory'];

				move_uploaded_file($tempName, $directory . $excryptImage);
				
		}
	}
?>